import Action from './Action'

export default class skuCount extends Action {
    columns: any
    constructor() {
        super()
        this.tableName = 'sku_count'

        this.columns = {
            id: 'INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL',
            userId: 'BIGINT(10)', // 账号id
            releaseId: 'BIGINT(10)',
            skuId: 'VARCHAR(10)',
            count: 'INT(8)',
            colorName: 'VARCHAR(10)' // 方便 寻找对应的颜色 减少循环
        }
    }

    // 查询商品的规格数据
    getSkuList(releaseId: string | number, userId: any) {
        return new Promise((resolve, reject) => {
            super
                .getList({
                    where: `where releaseId = ${releaseId} AND userId = ${userId}`
                })
                .then(res => {
                    resolve(res)
                })
                .catch(e => {
                    resolve([])
                })
        })
    }
    /**
     * 置零 未选择的规格
     * @param {arr}  当前有数据的sku数组
     */
    clearSkuCountNotSelect(userId: string | number, releaseId: string | number, arr: Array<any>) {
        let condition = ''
        if (arr.length > 0) {
            condition = ` and sku_id not IN (${arr.toString()})`
        }
        let a = this.executeOptions({
            model: this.QUERY_CUSTOM,
            sql: `UPDATE sku_count SET count = 0 where user_id = ${userId} and release_id = ${releaseId} ${condition}`
        })
        return this.execute(a)
    }
}
